<template>
  <div class="customer-opensea-container">
    <!-- <top-tip :tipcontent="tipcontent" /> -->
    <vab-query-form>
      <vab-query-form-left-panel :span="14">
        <el-form :inline="true" :model="queryForm" @submit.prevent>
          <el-space wrap :size="size">
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.cityArea"
                :options="cityOptions"
                :props="citySelect"
                placeholder="选择地区"
                collapse-tags
                clearable
              />
            </el-form-item>
            <el-form-item class="form-item">
              <el-cascader
                v-model="queryForm.industry"
                :options="industryOptions"
                :props="industrySelect"
                placeholder="选择行业"
                collapse-tags
                clearable
              />
            </el-form-item>
            <el-form-item>
              <el-input
                v-model.trim="queryForm.searchKey"
                clearable
                placeholder="请输入客户名称"
              />
            </el-form-item>
            <el-form-item>
              <el-button :icon="Search" type="primary" @click="queryData">
                查询
              </el-button>
            </el-form-item>
          </el-space>
        </el-form>
      </vab-query-form-left-panel>
      <!-- <vab-query-form-right-panel :span="10">
        <el-button :icon="Plus" type="primary" @click="handleEdit">
          添加一个新客户
        </el-button>
        <el-button :icon="Plus" type="primary" @click="handleImport">
          批量导入
        </el-button>
        <el-button :icon="Delete" type="danger" @click="handleAllocation">
          批量分配
        </el-button>
      </vab-query-form-right-panel> -->
    </vab-query-form>

    <el-table
      v-loading="listLoading"
      :data="list"
      @selection-change="setSelectRows"
    >
      <el-table-column align="center" show-overflow-tooltip type="selection" />
      <el-table-column
        align="center"
        label="客户ID"
        prop="CustomerID"
        show-overflow-tooltip
        width="80"
      />
      <el-table-column align="left" label="客户名称">
        <template #default="{ row }">
          <div style="cursor: pointer" @click="getCustomerInfo(row)">
            {{ row.CustomerAllName }}
          </div>
        </template>
      </el-table-column>
      <el-table-column
        align="left"
        label="所属区域"
        prop="CityShow"
        show-overflow-tooltip
      />
      <el-table-column
        align="left"
        label="所属行业"
        prop="IndustryShow"
        show-overflow-tooltip
      />
      <el-table-column
        align="left"
        label="地址"
        prop="Address"
        show-overflow-tooltip
      />
      <!-- <el-table-column align="center" label="查看详情" width="200">
        <template #default="{ row }">
          <el-button size="small" @click="getCustomerInfo(row)">
            查看详情
          </el-button>
        </template>
      </el-table-column> -->
      <el-table-column align="center" label="客户标记" width="200">
        <template #default="{ row }">
          <el-tag v-for="(itm, idx) in row.CustomerMarks" :key="idx">
            {{ itm }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" label="挑入" width="200">
        <template #default="{ row }">
          <el-button type="primary" @click="toDlog(row)">挑入</el-button>
        </template>
      </el-table-column>
      <!-- <el-table-column align="center" label="操作" width="300">
        <template #default="{ row }">
          <el-button text @click="handleAllocation(row)">分配</el-button>
          <el-button text @click="handleEdit(row)">编辑</el-button>
          <el-button text @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column> -->
    </el-table>
    <el-pagination
      background
      :current-page="queryForm.pageNo"
      :layout="layout"
      :page-size="queryForm.pageSize"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
    />
    <edit ref="editRef" @fetch-data="fetchData" />
    <import ref="importRef" />
    <allocation ref="allocationRef" @fetch-data="fetchData" />
    <business ref="businessRef" @fetch-data="fetchData" />
    <customer-allInfo ref="CustomerAllInfoRef" />
  </div>
</template>

<script>
  import { CitySelect, IndustrySelect } from '@/api/common'

  import CustomerAllInfo from '@/views/components/CustomerAllInfo.vue'
  import { getList, doDelete } from '@/api/customerOpensea'
  import Edit from './components/CustomerOpenseaEdit'
  import Import from './components/CustomerOpenseaImport'
  import Business from './components/business'
  import Allocation from './components/CustomerAllocation'
  // import TopTip from '@/components/TopTip'
  import { Delete, Plus, Search } from '@element-plus/icons-vue'

  export default defineComponent({
    name: 'Opensea',
    components: { Edit, Import, Allocation, Business, CustomerAllInfo },
    setup() {
      // const router = useRouter()
      const $baseConfirm = inject('$baseConfirm')
      const $baseMessage = inject('$baseMessage')

      const state = reactive({
        tipcontent: {
          title: '关于公海客户资源',
          memo:
            '公海，用于存储海量的客户资源；在此你可以尽可能的录入或导入越来越多的客户资源，以供市场销售人员进行客户的开发和营销工作；可以单个录入，也可以整理成标准的Execl文件进行批量导入；<br>' +
            '公海客户资源管理员可进行客户资源分配到某市场部或某销售人员；分配后此客户即成为内海可跟进客户。',
        },
        editRef: null,
        businessRef: null,
        importRef: null,
        allocationRef: null,
        areaSheng: 1,
        list: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        selectRows: '',
        CustomerAllInfoRef: null,
        cityOptions: [],
        industryOptions: [],
        citySelect: { checkStrictly: true, multiple: true },
        industrySelect: { checkStrictly: true, multiple: true },
        queryForm: {
          ServiceState: 0, // 从未签约
          cityArea: {},
          industry: {},
          searchKey: '',
          pageNo: 1,
          pageSize: 10,
        },
      })

      const setSelectRows = (val) => {
        state.selectRows = val
      }
      const applyPickin = (row) => {
        if (row.CustomerID) {
          $baseConfirm('你确定要挑入吗？', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        }
      }
      const handleEdit = (row) => {
        if (row.id) {
          state.editRef.showEdit(row)
        } else {
          state.editRef.showEdit()
        }
      }
      const handleImport = () => {
        state.importRef.showImport()
      }
      const handleAllocation = (row) => {
        if (row.CustomerID) {
          state.allocationRef.showAllocation(row)
        } else {
          state.allocationRef.showAllocation()
        }
      }
      const getCustomerInfo = (row) => {
        state.CustomerAllInfoRef.showEdit(row)
        // router.push({
        //   path: '/customer/Info',
        //   query: { customerid: row.CustomerID },
        // })
      }

      const getCitySelect = async () => {
        const {
          data: { cityOptions },
        } = await CitySelect()
        state.cityOptions = cityOptions
        console.log('列表1', cityOptions)
      }
      const getIndustrySelect = async () => {
        const {
          data: { industryOptions },
        } = await IndustrySelect()
        state.industryOptions = industryOptions
        console.log('列表2', industryOptions)
      }
      // 申请挑入弹出dlog
      const toDlog = (row) => {
        state.businessRef.showAllocation(row)
      }
      const handleDelete = (row) => {
        if (row.id) {
          $baseConfirm('你确定要删除当前项吗', null, async () => {
            const { msg } = await doDelete({ ids: row.id })
            $baseMessage(msg, 'success', 'vab-hey-message-success')
            await fetchData()
          })
        } else {
          if (state.selectRows.length > 0) {
            const ids = state.selectRows.map((item) => item.id).join()
            $baseConfirm('你确定要删除选中项吗', null, async () => {
              const { msg } = await doDelete({ ids })
              $baseMessage(msg, 'success', 'vab-hey-message-success')
              await fetchData()
            })
          } else {
            $baseMessage('未选中任何行', 'error', 'vab-hey-message-error')
          }
        }
      }
      const handleSizeChange = (val) => {
        state.queryForm.pageSize = val
        fetchData()
      }
      const handleCurrentChange = (val) => {
        state.queryForm.pageNo = val
        fetchData()
      }
      const queryData = () => {
        state.queryForm.pageNo = 1
        console.log('state.queryForm.city', state.queryForm.city)
        fetchData()
      }
      const fetchData = async () => {
        state.listLoading = true
        const {
          data: { list, total },
        } = await getList(state.queryForm)
        state.list = list
        state.total = total
        state.listLoading = false
      }
      onMounted(() => {
        fetchData()
        getCitySelect()
        getIndustrySelect()
      })

      return {
        ...toRefs(state),
        setSelectRows,
        toDlog,
        handleEdit,
        applyPickin,
        handleImport,
        handleAllocation,
        getCustomerInfo,
        getCitySelect,
        getIndustrySelect,
        handleDelete,
        handleSizeChange,
        handleCurrentChange,
        queryData,
        fetchData,
        Delete,
        Plus,
        Search,
      }
    },
  })
</script>

<style>
  .form-item {
    margin-right: 20px;
  }
</style>
